

package com.zdsx.modules.production.service.impl;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qcloud.cos.http.HttpMethodName;
import com.zdsx.common.constant.Constant;
import com.zdsx.common.page.PageData;
import com.zdsx.common.utils.ConvertUtils;
import com.zdsx.common.utils.PageUtils;
import com.zdsx.modules.oss.cloud.OSSFactory;
import com.zdsx.modules.oss.entity.SysOssEntity;
import com.zdsx.modules.production.dao.ProductInfoDao;
import com.zdsx.modules.production.dto.CategoryDTO;
import com.zdsx.modules.production.dto.ProductInfoDTO;
import com.zdsx.modules.production.entity.ProductInfo;
import com.zdsx.modules.production.service.ProductService;
import com.zdsx.modules.security.user.SecurityUser;
import com.zdsx.modules.security.user.UserDetail;
import com.zdsx.modules.sys.dto.SysDictDataDTO;
import com.zdsx.modules.sys.entity.DictData;
import com.zdsx.modules.sys.entity.DictType;
import com.zdsx.modules.sys.entity.SysDictDataEntity;
import com.zdsx.modules.sys.service.SysDictDataService;
import com.zdsx.modules.sys.service.SysDictTypeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * 异常日志
 *
 * @author Lr
 * @since 1.0.0
 */
@Service
public class ProductServiceImpl implements ProductService {

    @Resource
    private ProductInfoDao productInfoDao;

    @Resource
    private SysDictDataService sysDictDataService;

    @Resource
    private SysDictTypeService sysDictTypeService;

    /**
     * 获取产品列表
     * @return null
     */
    @Override
    public List<ProductInfoDTO> productList() {
        // 查询产品信息
        QueryWrapper<ProductInfo> wrapper = new QueryWrapper<>();
        // 根据用户userid查询产品信息
        List<ProductInfo> productInfoList = productInfoDao.selectList(wrapper);
        return ConvertUtils.sourceToTarget(productInfoList, ProductInfoDTO.class);
    }


    @Override
    public List<CategoryDTO> getProductCategoryList() {
        String categoryType = "product_type";
        // 获取到产品分类的字典
        DictType dictType = sysDictTypeService.getByType(categoryType);
        List<SysDictDataDTO> dictDataList = sysDictDataService.getByDictType(dictType.getId());
        List<CategoryDTO> categoryDTOS = new ArrayList<>();
        dictDataList.forEach(d -> {
            CategoryDTO categoryDTO = new CategoryDTO();
            categoryDTO.setCategorySign(d.getDictValue());// normal， relation
            categoryDTO.setCategoryName(d.getDictLabel()); // 气表联动，常规类
            categoryDTOS.add(categoryDTO);
        });
        return categoryDTOS;
    }

    @Override
    public PageData<ProductInfoDTO> page(Map<String, Object> params) {
        IPage<ProductInfo> page = productInfoDao.selectPage(
                PageUtils.getPage(params, Constant.CREATE_DATE, false),
                getWrapper(params)
        );
        return PageUtils.getPageData(page, ProductInfoDTO.class);
    }

    // http://81.71.135.254:8180/zdsx-admin/alarm/product/page?
    // order=&orderField=&page=1&limit=10&productCode=&productName=1&category=&productImage=&productDesc=&_t=1736130588679
    private QueryWrapper<ProductInfo> getWrapper(Map<String, Object> params){
        String category = (String) params.get("category");
        String productCode = (String) params.get("productCode");
        String productName = (String) params.get("productName");
        String productDesc = (String) params.get("productDesc");

        QueryWrapper<ProductInfo> wrapper = new QueryWrapper<>();
        wrapper.eq(StrUtil.isNotBlank(category), "category", category);
        wrapper.like(StrUtil.isNotBlank(productCode), "product_code", productCode);
        wrapper.like(StrUtil.isNotBlank(productName), "product_name", productName);
        wrapper.like(StrUtil.isNotBlank(productDesc), "product_desc", productDesc);

        return wrapper;
    }

    @Override
    public boolean insert(ProductInfoDTO dto) {
        String imageName = dto.getRealImageName();

        if (StrUtil.isNotBlank(imageName)) {
            String imageUrl = OSSFactory.build(Constant.QCLOUD_STORAGE_CONFIG_KEY).generatePresignedUrl(imageName, HttpMethodName.GET);
            dto.setProductImage(imageUrl);;
        }

        ProductInfo productInfoEntity = ConvertUtils.sourceToTarget(dto, ProductInfo.class);
        productInfoEntity.setCreator(SecurityUser.getUser().getId());
        int result = productInfoDao.insert(productInfoEntity);
        return result > 0;
    }

    @Override
    public boolean update(ProductInfoDTO productInfo) {
        ProductInfo productInfoEntity = ConvertUtils.sourceToTarget(productInfo, ProductInfo.class);
        int result = productInfoDao.updateById(productInfoEntity);
        return result > 0;
    }

    @Override
    public boolean delete(Long[] ids) {
        productInfoDao.deleteBatchIds(Arrays.asList(ids));
        return true;
    }

    @Override
    public ProductInfoDTO getById(Long id) {
        ProductInfo productInfo = productInfoDao.selectById(id);
        return ConvertUtils.sourceToTarget(productInfo, ProductInfoDTO.class);
    }


}